package org.adream.account.rest.api;

import java.util.List;

import org.adream.account.entity.EconomicTypeEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.EconomicTypeService;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/econ")
@Controller
public class EconomicTypeApiController {
	
	@Autowired
	private EconomicTypeService economicTypeService;
	
	private final static Logger logger = LoggerFactory.getLogger(EconomicTypeApiController.class);
	
	/**
	 * 新增
	 * @param economicType
	 * @return
	 */
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addEconomicType(@RequestBody EconomicTypeEntity economicType) {
		if(economicType == null || StringUtils.isEmpty(economicType.getEtname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return economicTypeService.addEconomicType(economicType);
	}
	
	/**
	 * 删除
	 * @param etid
	 * @return
	 */
	@RequestMapping(value = "/{etid}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteEconomicType(@PathVariable("etid") Integer etid) {
		if(etid == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		return economicTypeService.deleteEconomicType(etid);
	}
	
	/**
	 * 更新
	 * @param etid
	 * @param economicTypeEntity
	 * @return
	 */
	@RequestMapping(value = "/{etid}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<String> updateEconomicType(@PathVariable("etid") Integer etid,@RequestBody EconomicTypeEntity economicTypeEntity) {
		if(etid == null || economicTypeEntity == null || StringUtils.isEmpty(economicTypeEntity.getEtname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return economicTypeService.updateEconomicType(etid, economicTypeEntity);
	}
	
	/**
	 * 根据etid,删除标志查询
	 * @param etid
	 * @param dr
	 * @return
	 */
	@RequestMapping(value = "/etid/{etid}/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<EconomicTypeEntity> queryEconByEidAndDr(@PathVariable("etid") Integer etid,@PathVariable("dr") Integer dr) {
		if(etid == null || dr == null) {
			return new ResultModel<EconomicTypeEntity>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		EconomicTypeEntity economicType = economicTypeService.queryEconByEtidAndDr(etid, dr);
		if(economicType == null) {
			logger.warn("经济类型数据不存在,etid:{}", etid);
			return new ResultModel<EconomicTypeEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<EconomicTypeEntity>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, economicType);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<EconomicTypeEntity>> queryEconomicTypeByDr(@PathVariable("dr") Integer dr) {
		if(dr == null) {
			return new ResultModel<List<EconomicTypeEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<EconomicTypeEntity> economicTypes = economicTypeService.queryEconomicTypeByDr(dr);
		if(Utils.isEmptyCollection(economicTypes)) {
			return new ResultModel<List<EconomicTypeEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<EconomicTypeEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, economicTypes);
	}
}